/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import bean.EncounterBean;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import util.FormatUtil;
import util.HttpUtil;
import util.MakeTempalteStringUtil;
import org.hl7.fhir.dstu3.model.Patient;
import util.FhirContextContstant;

/**
 *
 * @author wudihua
 */
public class FhirEncounterGui extends javax.swing.JFrame {

    private static final Logger logger = LogManager.getLogger(FhirEncounterGui.class);

    
    private EncounterBean encounterBean;
 
    /**
     * Creates new form FhirPatientGui
     */
    public FhirEncounterGui() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jButton_create = new javax.swing.JButton();
        jButton_update = new javax.swing.JButton();
        jButton_query = new javax.swing.JButton();
        jButton_queryHistory = new javax.swing.JButton();
        jButton_delete = new javax.swing.JButton();
        jLabel_url = new javax.swing.JLabel();
        jTextField_url = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel_medicalInstitutionName = new javax.swing.JPanel();
        jLabel_encounterId = new javax.swing.JLabel();
        jLabel_historyStatus = new javax.swing.JLabel();
        jTextField_encounterId = new javax.swing.JTextField();
        jLabel_category = new javax.swing.JLabel();
        jTextField_category = new javax.swing.JTextField();
        jLabel_status = new javax.swing.JLabel();
        jLabel_typeAlias = new javax.swing.JLabel();
        jLabel_patientName = new javax.swing.JLabel();
        jTextField_patientName = new javax.swing.JTextField();
        jLabel_doctorAlias = new javax.swing.JLabel();
        jComboBox_doctorAlias = new javax.swing.JComboBox<>();
        jComboBox_typeAlias = new javax.swing.JComboBox<>();
        jLabel_typeName = new javax.swing.JLabel();
        jTextField_typeName = new javax.swing.JTextField();
        jLabel_doctorType = new javax.swing.JLabel();
        jTextField_doctorType = new javax.swing.JTextField();
        jLabel_doctorName = new javax.swing.JLabel();
        jTextField_doctorName = new javax.swing.JTextField();
        jLabel_stime = new javax.swing.JLabel();
        jTextField_stime = new javax.swing.JTextField();
        jLabel_etime = new javax.swing.JLabel();
        jTextField_etime = new javax.swing.JTextField();
        jLabel_reason = new javax.swing.JLabel();
        jTextField_reason = new javax.swing.JTextField();
        jLabel_diagnosisCondition = new javax.swing.JLabel();
        jTextField_diagnosisCondition = new javax.swing.JTextField();
        jLabel_diagnosisTypeAlias = new javax.swing.JLabel();
        jComboBox_diagnosisTypeAlias = new javax.swing.JComboBox<>();
        jLabel_diagnosisTypeName = new javax.swing.JLabel();
        jTextField_diagnosisTypeName = new javax.swing.JTextField();
        jLabel_diagnosisRank = new javax.swing.JLabel();
        jComboBox_diagnosisRank = new javax.swing.JComboBox<>();
        jLabel_originName = new javax.swing.JLabel();
        jTextField_originName = new javax.swing.JTextField();
        jLabel_originTypeAlias = new javax.swing.JLabel();
        jComboBox_originTypeAlias = new javax.swing.JComboBox<>();
        jLabel_originTypeName = new javax.swing.JLabel();
        jTextField_originTypeName = new javax.swing.JTextField();
        jLabel_destinationAlias = new javax.swing.JLabel();
        jComboBox_destinationAlias = new javax.swing.JComboBox<>();
        jLabel_destinationName = new javax.swing.JLabel();
        jTextField_destinationName = new javax.swing.JTextField();
        jLabel_serviceProvider = new javax.swing.JLabel();
        jTextField_medicalInstitutionName = new javax.swing.JTextField();
        jButton_clear = new javax.swing.JButton();
        jComboBox_status = new javax.swing.JComboBox<>();
        jLabel_refPatient = new javax.swing.JLabel();
        jTextField_refPatient = new javax.swing.JTextField();
        jLabel_refDoctor = new javax.swing.JLabel();
        jTextField_refDoctor = new javax.swing.JTextField();
        jLabel_refDiagnosis = new javax.swing.JLabel();
        jTextField_refDiagnosis = new javax.swing.JTextField();
        jLabel_refOrigin = new javax.swing.JLabel();
        jTextField_refOrigin = new javax.swing.JTextField();
        jLabel_refServiceProvider = new javax.swing.JLabel();
        jTextField_refServiceProvider = new javax.swing.JTextField();
        jTextField_historyStatus = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea_showXml = new javax.swing.JTextArea();
        jComboBox_dataType = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("就诊");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jButton_create.setText("创建");
        jButton_create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_createActionPerformed(evt);
            }
        });

        jButton_update.setText("更新");
        jButton_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_updateActionPerformed(evt);
            }
        });

        jButton_query.setText("检索");
        jButton_query.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_queryActionPerformed(evt);
            }
        });

        jButton_queryHistory.setText("获取历史信息");
        jButton_queryHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_queryHistoryActionPerformed(evt);
            }
        });

        jButton_delete.setText("删除");
        jButton_delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_deleteActionPerformed(evt);
            }
        });

        jLabel_url.setText("服务器URL：");

        jTextField_url.setText("http://36.33.216.237:9009/fhir/winningfhir/Encounter");

        jPanel_medicalInstitutionName.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel_encounterId.setText("资源ID：");

        jLabel_historyStatus.setText("就诊历史状态：");

        jLabel_category.setText("就诊名称：");

        jTextField_category.setText("张三的就诊");

        jLabel_status.setText("就诊状态：");

        jLabel_typeAlias.setText("就诊类型编码:");

        jLabel_patientName.setText("患者姓名：");

        jLabel_doctorAlias.setText("医生类型编码：");

        jComboBox_doctorAlias.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "ADM", "ATND", "CALLBCK", "CON", "DIS", "ESC", "REF", "SPRF", "PPRF", "PART", "translator", "emergency" }));

        jComboBox_typeAlias.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "IMP", "AMB", "EMER", "FLD", "HH", "ACUTE", "NONAC", "PRENC", "SS", "VR" }));

        jLabel_typeName.setText("就诊类型:");

        jTextField_typeName.setText("住院");

        jLabel_doctorType.setText("医生类型：");

        jTextField_doctorType.setText("主治医生");
        jTextField_doctorType.setToolTipText("");

        jLabel_doctorName.setText("医生姓名：");

        jTextField_doctorName.setText("李四");

        jLabel_stime.setText("入院日期：");

        jTextField_stime.setText("2018-04-18 10:12:21");

        jLabel_etime.setText("出院日期：");

        jTextField_etime.setText("2018-04-28 12:12:21");

        jLabel_reason.setText("就诊原因：");

        jTextField_reason.setText("髋关节置换术后康复治疗");

        jLabel_diagnosisCondition.setText("诊断原因：");

        jTextField_diagnosisCondition.setText("髋关节置换术后");

        jLabel_diagnosisTypeAlias.setText("诊断类型编码：");

        jComboBox_diagnosisTypeAlias.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "AD", "DD", "CC", "CM", "pre-op", "post-op", "billing" }));

        jLabel_diagnosisTypeName.setText("诊断类型：");

        jTextField_diagnosisTypeName.setText("入院诊断");

        jLabel_diagnosisRank.setText("诊断级别：");

        jComboBox_diagnosisRank.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "0", "1" }));

        jLabel_originName.setText("患者来源：");

        jTextField_originName.setText("北大国际医院");

        jLabel_originTypeAlias.setText("来源类型编码：");

        jComboBox_originTypeAlias.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "hosp-trans", "emd", "outp", "born", "gp", "mp", "nursing", "psych", "rehab", "other" }));

        jLabel_originTypeName.setText("来源类型：");

        jTextField_originTypeName.setText("转诊");
        jTextField_originTypeName.setToolTipText("");

        jLabel_destinationAlias.setText("去向类型编码：");

        jComboBox_destinationAlias.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "home", "other-hcf", "hosp", "long", "aadvice", "exp", "psy", "rehab", "snf", "oth" }));

        jLabel_destinationName.setText("去向类型：");

        jTextField_destinationName.setText("回家");

        jLabel_serviceProvider.setText("就诊医疗机构：");

        jTextField_medicalInstitutionName.setText("泰康燕园康复医院");

        jButton_clear.setText("清空");
        jButton_clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_clearActionPerformed(evt);
            }
        });

        jComboBox_status.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "finished", "planned", "arrived", "triaged", "in-progress", "onleave", "cancelled", "enter-in-error", "unknown" }));

        jLabel_refPatient.setText("关联患者ID：");

        jTextField_refPatient.setText("1");

        jLabel_refDoctor.setText("关联医生ID：");

        jTextField_refDoctor.setText("f201");

        jLabel_refDiagnosis.setText("关联诊断ID：");

        jTextField_refDiagnosis.setText("con1");

        jLabel_refOrigin.setText("关联医院ID：");

        jTextField_refOrigin.setText("loc1");

        jLabel_refServiceProvider.setText("关联机构ID：");

        jTextField_refServiceProvider.setText("serv1");

        jTextField_historyStatus.setText("generated");

        javax.swing.GroupLayout jPanel_medicalInstitutionNameLayout = new javax.swing.GroupLayout(jPanel_medicalInstitutionName);
        jPanel_medicalInstitutionName.setLayout(jPanel_medicalInstitutionNameLayout);
        jPanel_medicalInstitutionNameLayout.setHorizontalGroup(
            jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton_clear))
                    .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                        .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_historyStatus)
                                    .addComponent(jLabel_encounterId)
                                    .addComponent(jLabel_category)
                                    .addComponent(jLabel_status)
                                    .addComponent(jLabel_typeAlias)
                                    .addComponent(jLabel_patientName)
                                    .addComponent(jLabel_doctorAlias))
                                .addGap(58, 58, 58)
                                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBox_status, 0, 257, Short.MAX_VALUE)
                                    .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                        .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextField_category)
                                            .addComponent(jTextField_encounterId, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                                            .addComponent(jTextField_historyStatus))
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel_refPatient, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_typeName, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_originTypeAlias, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_destinationAlias, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField_typeName, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jComboBox_typeAlias, javax.swing.GroupLayout.Alignment.TRAILING, 0, 252, Short.MAX_VALUE))
                            .addComponent(jTextField_patientName, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField_refPatient, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                                    .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(jLabel_doctorType, javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addComponent(jLabel_doctorName))
                                                                                    .addComponent(jLabel_stime))
                                                                                .addComponent(jLabel_etime))
                                                                            .addComponent(jLabel_reason))
                                                                        .addComponent(jLabel_diagnosisCondition))
                                                                    .addGap(82, 82, 82))
                                                                .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                                    .addComponent(jLabel_diagnosisTypeAlias)
                                                                    .addGap(58, 58, 58)))
                                                            .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                                .addComponent(jLabel_diagnosisTypeName)
                                                                .addGap(82, 82, 82)))
                                                        .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                            .addComponent(jLabel_diagnosisRank)
                                                            .addGap(82, 82, 82)))
                                                    .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                        .addComponent(jLabel_originName)
                                                        .addGap(82, 82, 82)))
                                                .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                    .addComponent(jLabel_originTypeName)
                                                    .addGap(82, 82, 82)))
                                            .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                                .addComponent(jLabel_destinationName)
                                                .addGap(82, 82, 82)))
                                        .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                            .addComponent(jLabel_serviceProvider)
                                            .addGap(58, 58, 58)))
                                    .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                                        .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel_refDoctor)
                                            .addComponent(jLabel_refDiagnosis)
                                            .addComponent(jLabel_refOrigin)
                                            .addComponent(jLabel_refServiceProvider))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBox_doctorAlias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField_doctorType)
                                    .addComponent(jTextField_doctorName)
                                    .addComponent(jTextField_stime)
                                    .addComponent(jTextField_etime)
                                    .addComponent(jTextField_reason, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                                    .addComponent(jTextField_diagnosisCondition)
                                    .addComponent(jComboBox_diagnosisTypeAlias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField_diagnosisTypeName)
                                    .addComponent(jComboBox_diagnosisRank, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField_originName)
                                    .addComponent(jComboBox_originTypeAlias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField_originTypeName)
                                    .addComponent(jComboBox_destinationAlias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField_destinationName)
                                    .addComponent(jTextField_medicalInstitutionName)
                                    .addComponent(jTextField_refDoctor)
                                    .addComponent(jTextField_refDiagnosis)
                                    .addComponent(jTextField_refOrigin)
                                    .addComponent(jTextField_refServiceProvider))))
                        .addGap(0, 15, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel_medicalInstitutionNameLayout.setVerticalGroup(
            jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_medicalInstitutionNameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_encounterId)
                    .addComponent(jTextField_encounterId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_historyStatus)
                    .addComponent(jTextField_historyStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_category)
                    .addComponent(jTextField_category, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_status)
                    .addComponent(jComboBox_status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_typeAlias)
                    .addComponent(jComboBox_typeAlias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_typeName)
                    .addComponent(jTextField_typeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refPatient)
                    .addComponent(jTextField_refPatient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_patientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_patientName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_doctorAlias)
                    .addComponent(jComboBox_doctorAlias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_doctorType)
                    .addComponent(jTextField_doctorType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refDoctor)
                    .addComponent(jTextField_refDoctor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_doctorName)
                    .addComponent(jTextField_doctorName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_stime)
                    .addComponent(jTextField_stime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_etime)
                    .addComponent(jTextField_etime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_reason)
                    .addComponent(jTextField_reason, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_refDiagnosis)
                    .addComponent(jTextField_refDiagnosis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_diagnosisCondition)
                    .addComponent(jTextField_diagnosisCondition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox_diagnosisTypeAlias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_diagnosisTypeAlias))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_diagnosisTypeName)
                    .addComponent(jTextField_diagnosisTypeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_diagnosisRank)
                    .addComponent(jComboBox_diagnosisRank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refOrigin)
                    .addComponent(jTextField_refOrigin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_originName)
                    .addComponent(jTextField_originName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_originTypeAlias)
                    .addComponent(jComboBox_originTypeAlias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_originTypeName)
                    .addComponent(jTextField_originTypeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_destinationAlias)
                    .addComponent(jComboBox_destinationAlias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_destinationName)
                    .addComponent(jTextField_destinationName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_refServiceProvider)
                    .addComponent(jTextField_refServiceProvider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel_medicalInstitutionNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_serviceProvider)
                    .addComponent(jTextField_medicalInstitutionName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton_clear)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel_medicalInstitutionName);

        jTextArea_showXml.setColumns(20);
        jTextArea_showXml.setRows(5);
        jScrollPane2.setViewportView(jTextArea_showXml);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );

        jComboBox_dataType.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "xml", "json" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(61, 61, 61)
                                        .addComponent(jLabel7))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton_create)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_query)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_update)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_queryHistory)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton_delete)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel_url)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField_url, javax.swing.GroupLayout.PREFERRED_SIZE, 621, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addComponent(jComboBox_dataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_create)
                    .addComponent(jButton_query)
                    .addComponent(jButton_queryHistory)
                    .addComponent(jButton_delete)
                    .addComponent(jButton_update))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_url)
                    .addComponent(jTextField_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_dataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //创建
    private void jButton_createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_createActionPerformed
        //关联患者
        EncounterBean encounterBean = relatePatient(evt);
        
        String url = this.jTextField_url.getText();
        //获取发送数据的类型 xml/json
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        String encounterString = "";
        String result = "";
        
        if ("xml".equals(datatType)) {
            encounterString = MakeTempalteStringUtil.makeEncounterString(encounterBean, "/template/encounterTemplate.xml");
            encounterString = FormatUtil.formatXml(encounterString);
            logger.info("创建 encounter xml: {}", encounterString);
            result = HttpUtil.sendPost(url, encounterString, HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if ("json".equals(datatType)){
            encounterString = MakeTempalteStringUtil.makeEncounterString(encounterBean, "/template/json/encounterTemplate.json");
            logger.info("创建 encounter json: {}", encounterString);
            result = HttpUtil.sendPost(url, encounterString, HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        
        this.jTextArea_showXml.setText(result);
        logger.info("创建成功");
        //logger.info("http url:{},result:{}", url, result);
        
//        String encounterXml = MakeXmlUtil.makeEncounterXml(encounterBean);
//        encounterXml = FormatXmlUtil.formatXml(encounterXml);
//        logger.info("创建 encounterXml: {}", encounterXml);
//        String url = this.jTextField_url.getText();
//        String result = HttpUtil.sendPost(url, encounterXml);
//        result = FormatXmlUtil.formatXml(result);
//        this.jTextArea_showXml.setText(result);
//        logger.info("http url:{},result =", url, result);
        //this.jTextField_encounterId.setText();
        // 创建
    }//GEN-LAST:event_jButton_createActionPerformed
    
    //关联患者
    private EncounterBean relatePatient(java.awt.event.ActionEvent evt) {
        String url = this.jTextField_url.getText();
        //获取发送数据的类型 xml/json
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        encounterBean = new EncounterBean();
        //首先查询关联的id是否存在
        //关联患者 id
        String patientId = this.jTextField_refPatient.getText();
        encounterBean.setRefPatient(patientId);
        String baseUrl = url.substring(0, url.indexOf("Encounter"));
        String queryPatientUrl = baseUrl + "/Patient/" + patientId;
        String queryPatientResult = null;
        if ("xml".equals(datatType)) {
            queryPatientResult = HttpUtil.sendGet(queryPatientUrl,HttpUtil.ContentType_xml);
            if (queryPatientResult.contains("OperationOutcome")||queryPatientResult.length()<100) { //没对应患者id 报错
                this.jTextArea_showXml.setText(queryPatientResult);
                //结束
            }else{ //有对应患者id 赋值
                Patient patient = FhirContextContstant.getXMLParse().parseResource(Patient.class, queryPatientResult);
                encounterBean.setEncounterName(patient.getNameFirstRep().getText());
                //执行创建
                return setEncounterBean(evt, encounterBean);
            }
        }else if ("json".equals(datatType)){
            queryPatientResult = HttpUtil.sendGet(queryPatientUrl,HttpUtil.ContentType_json);
            if (queryPatientResult.contains("OperationOutcome")||queryPatientResult.length()<100) {
                this.jTextArea_showXml.setText(queryPatientResult);
                //结束
            }else{
                Patient patient = FhirContextContstant.getJsonParse().parseResource(Patient.class, queryPatientResult);
                encounterBean.setEncounterName(patient.getNameFirstRep().getText());
                //执行创建
                return setEncounterBean(evt, encounterBean);
            }
        }
        return null;
    }
    
    //赋值
    private EncounterBean setEncounterBean(java.awt.event.ActionEvent evt, EncounterBean encounterBean) {
        encounterBean.setEncounterId("001");
        encounterBean.setHistoryStatus(this.jTextField_historyStatus.getText());
        encounterBean.setCategory(this.jTextField_category.getText());
        
        encounterBean.setStatus(this.jComboBox_status.getSelectedItem().toString());
        
        encounterBean.setTypeAlias(this.jComboBox_typeAlias.getSelectedItem().toString());
        encounterBean.setTypeName(this.jTextField_typeName.getText());
        
        //encounterBean.setEncounterName(this.jTextField_patientName.getText());
        
        encounterBean.setDoctorAlias(this.jComboBox_doctorAlias.getSelectedItem().toString());
        encounterBean.setDoctorType(this.jTextField_doctorType.getText());
        encounterBean.setRefDoctor(this.jTextField_refDoctor.getText());
        encounterBean.setDoctorName(this.jTextField_doctorName.getText());

        try {
            Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.jTextField_stime.getText());
            Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.jTextField_etime.getText());
            //入院日期 2018-04-18T10:12:21Z
            encounterBean.setStime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(startDate));
            //出院日期 2018-04-18T10:12:21Z
            encounterBean.setEtime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(endDate));
        } catch (ParseException ex) {
            logger.error("error 入院日期,出院日期 日期转换错误，{}",ex);
        }

        encounterBean.setReason(this.jTextField_reason.getText());
        
        encounterBean.setRefDiagnosis(this.jTextField_refDiagnosis.getText());
        encounterBean.setDiagnosisCondition(this.jTextField_diagnosisCondition.getText());
        encounterBean.setDiagnosisTypeAlias(this.jComboBox_diagnosisTypeAlias.getSelectedItem().toString());
        encounterBean.setDiagnosisTypeName(this.jTextField_diagnosisTypeName.getText());
        encounterBean.setDiagnosisRank(this.jComboBox_diagnosisRank.getSelectedItem().toString());
        
        encounterBean.setRefOrigin(this.jTextField_refOrigin.getText());
        encounterBean.setOriginName(this.jTextField_originName.getText());
        encounterBean.setOriginTypeAlias(this.jComboBox_originTypeAlias.getSelectedItem().toString());
        encounterBean.setOriginTypeName(this.jTextField_originTypeName.getText());
        encounterBean.setDestinationAlias(this.jComboBox_destinationAlias.getSelectedItem().toString());
        encounterBean.setDestinationName(this.jTextField_destinationName.getText());
        
        encounterBean.setRefServiceProvider(this.jTextField_refServiceProvider.getText());
        encounterBean.setMedicalInstitutionName(this.jTextField_medicalInstitutionName.getText());
        
        return encounterBean;
    }
    
    //窗口关闭
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.setDefaultCloseOperation(FhirEncounterGui.DISPOSE_ON_CLOSE);
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    //检索
    private void jButton_queryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_queryActionPerformed
//        String url = this.jTextField_url.getText();
//        String encounterId = this.jTextField_encounterId.getText();
//        String patientName = this.jTextField_encounterName.getText();
//        String result = null;
//        if (!StringUtils.isEmpty(encounterId)) {
//            url = url + "/" + encounterId;
//            result = HttpUtil.sendGet(url);
//        } else if (!StringUtils.isEmpty(patientName)) {
//            try {   
//                patientName =  java.net.URLEncoder.encode(patientName,"utf-8");
//            } catch (UnsupportedEncodingException ex) {
//                java.util.logging.Logger.getLogger(FhirEncounterGui.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            url = url + "/" + patientName;
//            result = HttpUtil.sendGet(url);
//        }

        String url = this.jTextField_url.getText();
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        String result = null;

        if ("xml".equals(datatType)) {
            result = HttpUtil.sendGet(url,HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if("json".equals(datatType)) {
            result = HttpUtil.sendGet(url,HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        //logger.info("query url:{}, result:{} ",url, result);
        this.jTextArea_showXml.setText(result);
        //检索
    }//GEN-LAST:event_jButton_queryActionPerformed

    //历史查询
    private void jButton_queryHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_queryHistoryActionPerformed
          jButton_queryActionPerformed(evt);
          
//        String url = this.jTextField_url.getText();
//        //String encounterId = this.jTextField_encounterId.getText();
//        String result = null;
////        if (!StringUtils.isEmpty(encounterId)) {
////            url = url + "/" + encounterId;
////            result = HttpUtil.sendGet(url);
////        }
//        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
//        result = HttpUtil.sendGet(url);
//        
//        if (!StringUtils.isEmpty(result)) {
//            if ("xml".equals(datatType)) {
//                result = FormatXmlUtil.formatXml(result);
//            } else if("json".equals(datatType)) {
//                 result = FormatJsonUtil.formatJson(result);
//            }
//            logger.info("query url result {} ", result);
//             this.jTextArea_showXml.setText(result);
//        }else{
//            this.jTextArea_showXml.setText("查询结果为空");
//        }
       
      
        // 获取历史版本
    }//GEN-LAST:event_jButton_queryHistoryActionPerformed

    //删除
    private void jButton_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_deleteActionPerformed
        //jButton_queryActionPerformed(evt);
       
//        String encounterId = this.jTextField_encounterId.getText().trim();
//
//        if (!StringUtils.isEmpty(encounterId)) {
//            url = url + "/" + encounterId;
//            HttpUtil.sendGet(url);
//            this.jTextArea_showXml.setText("删除成功");
//        }
        String url = this.jTextField_url.getText().trim();
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        String result = null;
        if ("xml".equals(datatType)) {
            result = HttpUtil.sendDelete(url,HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if("json".equals(datatType)) {
            result = HttpUtil.sendDelete(url,HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        //logger.info("delete url:{}, result {} ",url, result);
        this.jTextArea_showXml.setText(result);
        
        // 删除
    }//GEN-LAST:event_jButton_deleteActionPerformed

    //更新
    private void jButton_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_updateActionPerformed
//        encounterBean = new EncounterBean();
//        encounterBean.setEncounterId(this.jTextField_encounterId.getText());
//        encounterBean.setHistoryStatus(this.jTextField_historyStatus.getText());
//        encounterBean.setCategory(this.jTextField_category.getText());
//        
//        encounterBean.setStatus(this.jComboBox_status.getSelectedItem().toString());
//        
//        encounterBean.setTypeAlias(this.jComboBox_typeAlias.getSelectedItem().toString());
//        encounterBean.setTypeName(this.jTextField_typeName.getText());
//        
//        encounterBean.setRefPatient(this.jTextField_refPatient.getText());
//        encounterBean.setEncounterName(this.jTextField_patientName.getText());
//        
//        encounterBean.setDoctorAlias(this.jComboBox_doctorAlias.getSelectedItem().toString());
//        encounterBean.setDoctorType(this.jTextField_doctorType.getText());
//        encounterBean.setRefDoctor(this.jTextField_refDoctor.getText());
//        encounterBean.setDoctorName(this.jTextField_doctorName.getText());
//        
//        encounterBean.setStime(this.jTextField_stime.getText());
//        encounterBean.setEtime(this.jTextField_etime.getText());
//        
//        encounterBean.setReason(this.jTextField_reason.getText());
//        
//        encounterBean.setRefDiagnosis(this.jTextField_refDiagnosis.getText());
//        encounterBean.setDiagnosisCondition(this.jTextField_diagnosisCondition.getText());
//        encounterBean.setDiagnosisTypeAlias(this.jComboBox_diagnosisTypeAlias.getSelectedItem().toString());
//        encounterBean.setDiagnosisTypeName(this.jTextField_diagnosisTypeName.getText());
//        encounterBean.setDiagnosisRank(this.jComboBox_diagnosisRank.getSelectedItem().toString());
//        
//        encounterBean.setRefOrigin(this.jTextField_refOrigin.getText());
//        encounterBean.setOriginName(this.jTextField_originName.getText());
//        encounterBean.setOriginTypeAlias(this.jComboBox_originTypeAlias.getSelectedItem().toString());
//        encounterBean.setOriginTypeName(this.jTextField_originTypeName.getText());
//        encounterBean.setDestinationAlias(this.jComboBox_destinationAlias.getSelectedItem().toString());
//        encounterBean.setDestinationName(this.jTextField_destinationName.getText());
//        
//        encounterBean.setRefServiceProvider(this.jTextField_refServiceProvider.getText());
//        encounterBean.setMedicalInstitutionName(this.jTextField_medicalInstitutionName.getText());
        
         //关联患者
        EncounterBean encounterBean = relatePatient(evt);
        encounterBean.setEncounterId(this.jTextField_encounterId.getText());
        String url = this.jTextField_url.getText();
        //获取发送数据的类型 xml/json
        String datatType = this.jComboBox_dataType.getSelectedItem().toString();
        String encounterString = "";
        String result = "";
        if ("xml".equals(datatType)) {
            encounterString = MakeTempalteStringUtil.makeEncounterString(encounterBean, "/template/encounterTemplate.xml");
            encounterString = FormatUtil.formatXml(encounterString);
            logger.info("更新 encounter xml: {}", encounterString);
            result = HttpUtil.sendPut(url, encounterString, HttpUtil.ContentType_xml);
            result = FormatUtil.formatXml(result);
        } else if ("json".equals(datatType)){
            encounterString = MakeTempalteStringUtil.makeEncounterString(encounterBean, "/template/json/encounterTemplate.json");
            logger.info("更新 encounter json: {} ", encounterString);
            result = HttpUtil.sendPut(url, encounterString, HttpUtil.ContentType_json);
            result = FormatUtil.formatJson(result);
        }
        
        this.jTextArea_showXml.setText(result);
        //logger.info("http url:{},result:{}", url, result);
        
//        String encounterXml = MakeXmlUtil.makeEncounterXml(encounterBean);
//        encounterXml = FormatXmlUtil.formatXml(encounterXml);
//        logger.info("更新 encounterXml: {}", encounterXml);
//        
//        String url = this.jTextField_url.getText();
//        String result = HttpUtil.sendPut(url, encounterXml);
//        result = FormatXmlUtil.formatXml(result);
//        this.jTextArea_showXml.setText(result);
        logger.info("更新成功");
        // 更新
    }//GEN-LAST:event_jButton_updateActionPerformed

    //清除
    private void jButton_clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_clearActionPerformed
        this.jTextField_category.setText("");
        this.jTextField_typeName.setText("");
        this.jTextField_patientName.setText("");
        this.jTextField_doctorType.setText("");
        this.jTextField_doctorType.setText("");
        this.jTextField_stime.setText("");
        this.jTextField_etime.setText("");
        this.jTextField_reason.setText("");
        this.jTextField_diagnosisCondition.setText("");
        this.jTextField_destinationName.setText("");
        this.jTextField_originName.setText("");
        this.jTextField_originTypeName.setText("");
        this.jTextField_destinationName.setText("");
        this.jTextField_medicalInstitutionName.setText("");
        this.jTextField_refPatient.setText("");
        this.jTextField_refDoctor.setText("");
        this.jTextField_refDiagnosis.setText("");
        this.jTextField_refOrigin.setText("");
        this.jTextField_refServiceProvider.setText("");
        // 清空
    }//GEN-LAST:event_jButton_clearActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FhirEncounterGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FhirEncounterGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FhirEncounterGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FhirEncounterGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FhirEncounterGui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_clear;
    private javax.swing.JButton jButton_create;
    private javax.swing.JButton jButton_delete;
    private javax.swing.JButton jButton_query;
    private javax.swing.JButton jButton_queryHistory;
    private javax.swing.JButton jButton_update;
    private javax.swing.JComboBox<String> jComboBox_dataType;
    private javax.swing.JComboBox<String> jComboBox_destinationAlias;
    private javax.swing.JComboBox<String> jComboBox_diagnosisRank;
    private javax.swing.JComboBox<String> jComboBox_diagnosisTypeAlias;
    private javax.swing.JComboBox<String> jComboBox_doctorAlias;
    private javax.swing.JComboBox<String> jComboBox_originTypeAlias;
    private javax.swing.JComboBox<String> jComboBox_status;
    private javax.swing.JComboBox<String> jComboBox_typeAlias;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel_category;
    private javax.swing.JLabel jLabel_destinationAlias;
    private javax.swing.JLabel jLabel_destinationName;
    private javax.swing.JLabel jLabel_diagnosisCondition;
    private javax.swing.JLabel jLabel_diagnosisRank;
    private javax.swing.JLabel jLabel_diagnosisTypeAlias;
    private javax.swing.JLabel jLabel_diagnosisTypeName;
    private javax.swing.JLabel jLabel_doctorAlias;
    private javax.swing.JLabel jLabel_doctorName;
    private javax.swing.JLabel jLabel_doctorType;
    private javax.swing.JLabel jLabel_encounterId;
    private javax.swing.JLabel jLabel_etime;
    private javax.swing.JLabel jLabel_historyStatus;
    private javax.swing.JLabel jLabel_originName;
    private javax.swing.JLabel jLabel_originTypeAlias;
    private javax.swing.JLabel jLabel_originTypeName;
    private javax.swing.JLabel jLabel_patientName;
    private javax.swing.JLabel jLabel_reason;
    private javax.swing.JLabel jLabel_refDiagnosis;
    private javax.swing.JLabel jLabel_refDoctor;
    private javax.swing.JLabel jLabel_refOrigin;
    private javax.swing.JLabel jLabel_refPatient;
    private javax.swing.JLabel jLabel_refServiceProvider;
    private javax.swing.JLabel jLabel_serviceProvider;
    private javax.swing.JLabel jLabel_status;
    private javax.swing.JLabel jLabel_stime;
    private javax.swing.JLabel jLabel_typeAlias;
    private javax.swing.JLabel jLabel_typeName;
    private javax.swing.JLabel jLabel_url;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_medicalInstitutionName;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea_showXml;
    private javax.swing.JTextField jTextField_category;
    private javax.swing.JTextField jTextField_destinationName;
    private javax.swing.JTextField jTextField_diagnosisCondition;
    private javax.swing.JTextField jTextField_diagnosisTypeName;
    private javax.swing.JTextField jTextField_doctorName;
    private javax.swing.JTextField jTextField_doctorType;
    private javax.swing.JTextField jTextField_encounterId;
    private javax.swing.JTextField jTextField_etime;
    private javax.swing.JTextField jTextField_historyStatus;
    private javax.swing.JTextField jTextField_medicalInstitutionName;
    private javax.swing.JTextField jTextField_originName;
    private javax.swing.JTextField jTextField_originTypeName;
    private javax.swing.JTextField jTextField_patientName;
    private javax.swing.JTextField jTextField_reason;
    private javax.swing.JTextField jTextField_refDiagnosis;
    private javax.swing.JTextField jTextField_refDoctor;
    private javax.swing.JTextField jTextField_refOrigin;
    private javax.swing.JTextField jTextField_refPatient;
    private javax.swing.JTextField jTextField_refServiceProvider;
    private javax.swing.JTextField jTextField_stime;
    private javax.swing.JTextField jTextField_typeName;
    private javax.swing.JTextField jTextField_url;
    // End of variables declaration//GEN-END:variables
}
